package com.waiting.mq.controller;

import com.waiting.mq.entity.Order;
import com.waiting.mq.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2020-09-29 10:37:58
 */
@CrossOrigin
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public Order selectOne(@PathVariable Integer id) {
        return this.orderService.queryById(id);
    }

    /**
     * 下訂單
     * @return
     */
    @PostMapping
    public Map subimt() {


        Integer orderId = orderService.submit();


        Map<String, Object> result = new HashMap();
        result.put("status", 1);
        result.put("msg", "訂單下訂成功");
        result.put("orderId", orderId);

        return result;
    }

    @GetMapping("/pay/{id}")
    public Map pay(@PathVariable Integer id) {

        Integer resultCode = orderService.pay(id);

        Map<String, Object> result = new HashMap();
        result.put("status", resultCode);

        return result;
    }

    @GetMapping("/list")
    public List list() {

        List<Order> orders = orderService.queryAllByLimit(0, 100);

        return orders;
    }
}
